package com.dgut.web;


import com.dgut.adminmanager.entity.Administrator;
import com.dgut.adminmanager.service.AdminService;
import com.dgut.springsecurity.Details.AdminDetail;
import com.dgut.usermanager.entity.UserAccount;
import com.dgut.usermanager.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AdministratorController {

    @Autowired
    AdminService adminService;
    @Autowired
    UserAccountService userAccountService;

    // 后台-登录界面
    @GetMapping("/admin/login_backend")
    //@ResponseBody
    public String adminWxLogin() {
        return "index";
    }

    // 后台-首页
    @GetMapping("/admin")
    @ResponseBody
    public Administrator adminWxIndex() {
        AdminDetail adminDetail=(AdminDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Administrator administrator=adminService.findAdministratorByAccount(adminDetail.getUsername());
        return administrator;
    }

    @PostMapping("admin/browser")
    public String adminIndex(Model model) {
        AdminDetail adminDetail=(AdminDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Administrator administrator=adminService.findAdministratorByAccount(adminDetail.getUsername());
        model.addAttribute("admin",administrator);
        return "admin";
    }
@GetMapping("/admin/query")
    public String adminQuery()
    {

        return "admin";
    }

    @GetMapping("/admin/update")
    public String adminUpdate(Administrator administrator, Model model) {
        adminService.updateAdministrator(administrator);
        return "admin/admin";
    }
    @GetMapping("/admin/user/query")
    public String adminUserQuery(Model model) {
        List<UserAccount> userAccounts=userAccountService.findAllUserAccount();
        model.addAttribute("userAccounts",userAccounts);
        return "admin/user/query";
    }
    @GetMapping("/admin/user/delete")
    public String adminUserDelete(@RequestParam(name="userAccountId") String userAccountId) {
        Integer id=Integer.parseInt(userAccountId);
        userAccountService.deleteUserAccountById(id);
        return "admin/user/query";
    }
}
